//
//  ViewController.m
//  Map
//
//  Created by peoit-xq on 15/12/18.
//  Copyright © 2015年 apple. All rights reserved.
//

#import "ViewController.h"
#import  <MAMapKit/MAMapKit.h>
#import "MapMananger.h"
#import "Map.h"
@interface ViewController ()<MAMapViewDelegate,AMapSearchDelegate,MapManangerDelegate>{

//    NSArray *array;
    
    AMapSearchAPI *_searchAPI;
    MAPointAnnotation *annotation;
    MAPointAnnotation *annotation1;
    
    MapMananger *_map;
    
}
@property (nonatomic, strong)MAMapView *mapView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //显示地图
    [self initViews];
    
}
- (void)viewDidDisappear:(BOOL)animated{
    self.mapView.delegate = nil;
    _searchAPI.delegate = nil;
}

#pragma mark init
- (void)initViews{

    self.mapView = [[MAMapView alloc] initWithFrame:CGRectMake(0.0, 0.0, WIDTH,HEIGHT)];
    self.mapView.delegate = self;
    self.mapView.showsScale = NO;//不显示比例
    self.mapView.mapType = MAMapTypeStandard;
    self.mapView.backgroundColor = [UIColor clearColor];
    self.mapView.userTrackingMode = MAUserTrackingModeFollow;//定位用户位置的模式
    self.mapView.showsCompass = NO;
    self.mapView.rotateEnabled = NO;
    self.mapView.rotateCameraEnabled = NO;
    [self.mapView setZoomLevel:12 animated:YES];
    self.mapView.logoCenter = CGPointMake(200.0, 0.0);//logo位置
    self.mapView.customizeUserLocationAccuracyCircleRepresentation = YES;
    //self.mapView.visibleMapRect = MAMapRectMake(220880104, 101476980, 272496, 466656);//可见范围
    [self.view addSubview:self.mapView];
        
    
    //创建并显示大头针
    annotation = [[MAPointAnnotation alloc]init];
    CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(29.53772655, 106.50685072);
    annotation.coordinate = coord;
    [self.mapView addAnnotation:annotation];
    
    annotation1 = [[MAPointAnnotation alloc]init];
//    CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(38.487108, 106.233474);
    annotation1.coordinate = coord;
    [self.mapView addAnnotation:annotation1];

    
    _map = [[MapMananger alloc]init];
    CLLocation *location = [[CLLocation alloc]initWithLatitude:38.487108 longitude:106.233474];
//    1.委托
//    c.delegate = self;
//    [c AntiCompilationWithLocation:location];
//    2.block
    [_map AntiCompilationWithLocation:location completion:^(NSString *address) {
        NSLog(@"%@",address);
    }];
    
    [_map CompilationWithLocation:@"重庆市渝中区" completion:^(CLLocation *location) {
        NSLog(@"%@",location);
    }];

    _map.userCity = @"重庆";
    [_map POIKeywordsSearchWithString:@"龙湖" completion:^(NSArray *array) {
        [self POIKeywordsSearchResult:array];
    }];

    

}

- (void)POIKeywordsSearchResult:(NSArray *)array {
    NSLog(@"POIKeywordsSearchResult:%@",array);
}

//- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation{
//    
//    if ([annotation isKindOfClass:[MAPointAnnotation class]])
//    {
//        static NSString *reuseIndetifier = @"annotationReuseIndetifier";
//        MAAnnotationView *annotationView = (MAAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseIndetifier];
//        if (annotationView == nil)
//        {
//            annotationView = [[MAAnnotationView alloc] initWithAnnotation:annotation
//                                                          reuseIdentifier:reuseIndetifier];
//        }
//        
//         annotationView.image = [UIImage imageNamed:@"annotationImg.png"];
//        //设置中⼼心点偏移，使得标注底部中间点成为经纬度对应点
////        annotationView.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.3];
////        annotationView.centerOffset = CGPointMake(100, -18);
//        return annotationView;
//    }
//    return nil;
//}

#pragma mark CompilationDelegate
- (void)compilation:(MapMananger *)com returnAntiCompilationResult:(NSString *)address{

    NSLog(@"%@",address);
}
- (void)compilation:(MapMananger *)com returnCompilationResult:(CLLocation *)location{

    NSLog(@"%.6lf,%.6lf",location.coordinate.latitude,location.coordinate.longitude);
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
